var app = getApp();
var canvas = require('../../../utils/canvas.js');

var selects = [{
  "id": 1,
  "title": "全部运动",
  "key": "totalkcal",
  "unit": "kcal",
  "sportsType": 7,
}, {
  "id": 2,
  "title": "步行",
  "key": "kcal",
  "unit": "kcal",
  "sportsType": 1,
}, {
  "id": 3,
  "title": "有氧健身",
  "key": "kcal",
  "unit": "kcal",
  "sportsType": 2,
}, {
  "id": 4,
  "title": "力量健身",
  "key": "kcal",
  "unit": "kcal",
  "sportsType": 3,
},
//  {
//   "id": 5,
//   "title": "健身心率",
//   "key": "heartRate",
//   "unit": "bpm",
//   "sportsType": 4,
// },
//  {
//   "id": 6,
//   "title": "睡眠记录",
//   "key": "duration",
//   "unit": "小时",
//   "sportsType": 5,
// }, {
//   "id": 7,
//   "title": "饮食摄入",
//   "key": "kcal",
//   "unit": "kcal",
//   "sportsType": 6,
// }
];

var navDatas = [{
  id: 1,
  title: "日",
  countType: 1,
}, {
  id: 2,
  title: "周",
  countType: 2,
}, {
  id: 3,
  title: "月",
  countType: 3,
}, {
  id: 4,
  title: "年",
  countType: 4,
}]

Page({
  data: {
    selects: selects, //运动类型列表
    selectIndex: 0, //当前查看运动类型下标
    navDatas: navDatas, //周期选择类型列表
    navDataIndex: 0, //当前选中周期类型下标
    isShow: false, //是否弹框
  },
  onLoad: function(options) {
    var that = this;
    var index = options && options.index || 0;
    var time = options && options.time || "2019-01-01";
    that.setData({
      selectIndex: index,
      time: time,
      navDataIndex: 0,
    })
    setNavStyle(that);
    that.dataReq();
  },
  /**
   * 返回按钮
   */
  navigationBack: function() {
    wx.navigateBack()
  },
  /**
   * 关闭模态框
   */
  closeModal: function(e) {
    var that = this;
    that.setData({
      isShow: false,
    })
  },
  /**
   * 选择运动类型
   */
  selChange: function(e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    that.setData({
      selectIndex: index,
      navDataIndex: 0,
    })
    that.dataReq();
    that.closeModal();
  },
  /**
   * 打开选择运动类型
   */
  openChange: function() {
    var that = this;
    that.setData({
      isShow: true,
    })
  },
  /**
   * 选择周期类型
   */
  navChange: function(e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    that.setData({
      navDataIndex: index,
    })
    that.dataReq();
  },
  /**
   * 选中当前运动类型
   */
  dataReq: function() {
    var that = this;
    var selectIndex = that.data.selectIndex;
    var navDatas = that.data.navDatas;
    var navDataIndex = that.data.navDataIndex;
    var countType = navDatas[navDataIndex].countType;
    var key = that.data.selects[that.data.selectIndex].key;
    var unit = that.data.selects[that.data.selectIndex].unit;
    var sportsType = that.data.selects[that.data.selectIndex].sportsType;
    var time = that.data.time;

    var reqData = {
      countType,
      sportsType,
      time,
    };

    app.middleware.requestAjax({
      serAddr: app.serAddrs.sportDetail,
      data: reqData,
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function(res) {
        console.log(res)
        // if (res.status == 1) {
        var lists = [];
        var showIndex = 0;
        if (selectIndex == 1) { //步行
          lists = res.data.reverse();
          showIndex = lists.length - 1;
        } else if (selectIndex == 2) { //力量
          lists = res.data.reverse();
          showIndex = lists.length - 1;
        } else if (selectIndex == 3) { //有氧
          lists = res.data.reverse();
          showIndex = lists.length - 1;
        } else if (selectIndex == 4) { //心率
          lists = res.data.HeartRates.reverse();
          showIndex = lists.length - 1;
        } else if (selectIndex == 5) { //睡眠
          lists = res.data.sleepDatas.reverse();
          showIndex = lists.length - 1;
        } else if (selectIndex == 6) { //饮食
          lists = res.data.reverse();
          showIndex = lists.length - 1;
        } else { //全部运动
          lists = res.data.total.reverse();
          showIndex = lists.length - 1;
        }

        that.drawCanvas(lists, key, unit, showIndex);

        that.setData({
          lists: lists,
          showIndex: showIndex,
        })
        // }
      }
    })
  },
  drawCanvas: function(list, key, unit, showIndex) {
    var winWidth = wx.getSystemInfoSync().windowWidth;
    //画布
    canvas.switch_canvas(list, key, unit, showIndex, winWidth);
  },
  onCavas: function(e) {
    //封装好的canvas
    var that = this;
    var key = that.data.selects[that.data.selectIndex].key;
    var unit = that.data.selects[that.data.selectIndex].unit;
    var winWidth = wx.getSystemInfoSync().windowWidth;
    var lists = that.data.lists;
    canvas.onCavas(e, lists, key, unit, lists.length - 1, winWidth, function(res) {
      that.setData({
        showIndex: res
      })
    });
  },
})

function setNavStyle(that) {
  var temp = {};
  var systemInfo = wx.getSystemInfoSync();
  console.warn(systemInfo)
  var statusBarHeight = systemInfo.statusBarHeight;
  var screenHeight = systemInfo.screenHeight;
  var screenWidth = systemInfo.screenWidth;
  var model = systemInfo.model.slice(0, 6);
  if (statusBarHeight >= 25 && model == "iPhone") {
    temp["isPaddingFoot"] = true;
  }
  temp["statusBarHeight"] = statusBarHeight;
  temp["canvas_height"] = screenHeight;
  temp["canvas_width"] = screenWidth;
  that.setData(temp);
}